#include "./../inc/file.h"

file_node_p file_node_init()
{
    file_node_p headNode = (file_node_p)malloc(sizeof(file_node_t));
    headNode->prev = headNode;
    headNode->next = headNode;
    strcpy(headNode->name, "head");
    strcpy(headNode->path, "head");
    return headNode;
}

void file_node_create(file_node_p headNode, char *name, char *path)
{
    file_node_p p = headNode;
    while (p->next != headNode)
    {
        p = p->next;
    }
    file_node_p newNode = (file_node_p)malloc(sizeof(file_node_t));

    strcpy(newNode->name, name);
    strcpy(newNode->path, path);
    strcat(newNode->path, "/");

    p->next = newNode;
    newNode->prev = p;
    newNode->next = headNode;
    headNode->prev = newNode;
}

void print_file_node(file_node_p headNode)
{
    file_node_p p = headNode->next;
    while (p != headNode)
    {
        printf("name:%s\t path:%s\n", p->name, p->path);
        p = p->next;
    }
}

file_node_p find_file_node(file_node_p headNode, int currIMG)
{
    int i = 0;
    file_node_p p = headNode->next;
    while (p != headNode)
    {
        if (i == currIMG)
        {
            return p;
        }
        i++;
        p = p->next;
    }
    return p;
}

int get_file_node_num(file_node_p headNode)
{
    int count = 0;
    file_node_p p = headNode->next;
    while (p != headNode)
    {
        count++;
        p = p->next;
    }
    return count;
}